define(function () {
    var module = function () {
        this.initPage();
    };
    module.prototype = {
        swiper:'',
        // 功能描述：初始化
        init: function () {
            this.load();
        },

        pageData: {
            left_classify: {
                list: [],
                images: {},
            },
            right_classify: {
                images: [],
                naterialList: [],
                class: {},
            }
        },
        swiper: {},
        pageSize: 30,

        initLeftImg: function(id, callback) {
            var _this = this;
            if (id != 0 && _this.pageData.left_classify.images[id] == null) {
                $.get(config.host + "/api/youbaoDiy/productImageList?id="+id+"&page=1&size="+_this.pageSize, function(data2, status2) {
                    _this.pageData.left_classify.images[id] = data2.data;
                    var html = $.template('left_classifyImgTpl', {images: _this.pageData.left_classify.images[id]});
                    document.getElementById('left_classifyImg' + id).innerHTML = html;
                    callback();
                });
            }
            callback();
        },

        initRightClass: function(id, callback) {
            var _this = this;
            if (id != 0 && _this.pageData.right_classify.class[id] == null) {
                $.get(config.host + "/api/youbaoDiy/naterialClassList?id="+id+"&page=1&size="+_this.pageSize, function (data2, status2) {
                    _this.pageData.right_classify.class[id] = data2.data;
                    var html = $.template('naterialClassTpl', { nClass: _this.pageData.right_classify.class[id] });
                    document.getElementById('naterialClass' + id).innerHTML = html;
                    callback();
                    if(data2.data.length <= 0 ){
                        document.getElementById('naterialClass' + id).parentNode.style.display = "none";
                    }
                });
            }
            callback();
        },
        
        initRightIP: function(id, callback) {
            var _this = this;
            if (id != 0 && _this.pageData.right_classify.class[id] == null) {
                $.get(config.host + "/api/youbaoDiy/naterialClassImageList?id="+id+"&page=1&size="+_this.pageSize, function (data2, status2) {
                
                });
            }
            callback();
        },

        initPage: function() {
            var _this = this;
            $.get(config.host + "/api/youbaoDiy/naterialList?page=1&size="+_this.pageSize, function (naterial) {
                _this.pageData.right_classify.naterialList = naterial.data;

                $.get(config.host + "/api/youbaoDiy/shadeList?page=1&size="+_this.pageSize, function(shade) {
                    _this.pageData.right_classify.images = shade.data;
                    var html = $.template('right_classifyTpl', _this.pageData.right_classify);
                    document.getElementById('right_classify').innerHTML = html;

                    $.get(config.host + "/api/youbaoDiy/productList?page=1&size="+_this.pageSize, function(data, status) {
                        _this.pageData.left_classify.list = data.data;
                        var html = $.template('left_classifyTpl', _this.pageData.left_classify);
                        document.getElementById('left_classify').innerHTML = html;
                        var id = '0';
                        if (data.data.length > 0) {
                            _this.initLeftImg(data.data[0].id, function(){
                                _this.init();
                            });
                        }else{
                            _this.initLeftImg(id, function(){
                                _this.init();
                            });
                        }
                    });
                });
            });
        },

        // 功能描述：加载
        load: function () {

            this.event();
            try{this.showSelectItem($("ul#left_classify li.one_level.on")[0]);}catch(e){console.log(e);}
            try{this.showSelectItem($("ul#right_classify li.one_level.on")[0]);}catch(e){console.log(e);}
            
        },

        // 功能描述：加载
        event: function () {
            var _this = this;
            $("ul.classify li.one_level span.classify_title").off().on("click",function(){
                var __this = this;
                var obj = __this.parentNode;
                var selected_obj = obj.parentNode.querySelector("li.one_level.on");

                function run() {
                    if (obj === selected_obj){
                        obj.classList.remove("on");
                        __this.querySelector("i.fa").classList.add("fa-rotate-180");
                        return true;
                    }
                    if (!selected_obj){
                        obj.classList.add("on");
                        __this.querySelector("i.fa").classList.remove("fa-rotate-180");
                        _this.showSelectItem(obj.parentNode.id === "left_classify" ? obj.parentNode : obj);
                        return true;
                    }
                    selected_obj.classList.remove("on");
                    obj.classList.add("on");
                    selected_obj.querySelector("i.fa").classList.add("fa-rotate-180");
                    __this.querySelector("i.fa").classList.remove("fa-rotate-180");
                    _this.showSelectItem(obj.parentNode.id === "left_classify" ? obj.parentNode : obj);
                }
                
                if ($(__this.parentNode).attr('link') != null && $(__this.parentNode).attr('ref') == 'left_classify') {
                    _this.initLeftImg($(__this.parentNode).attr('link'), function() {
                        run();
                    });
                } else if ($(__this.parentNode).attr('link') != null && $(__this.parentNode).attr('ref') == 'right_classify') {
                    _this.initRightClass($(__this.parentNode).attr('link'), function() {
                        run();
                    });
                }else {
                    run();
                }
            })
        },
        showSelectItem:function(obj){
            var nav_classify = obj.querySelector("div.nav_classify");
            if(nav_classify){
                // 执行方法
                this.swiper[nav_classify.id] = new Swiper("#" + nav_classify.id, {
                    slidesPerView: 'auto',
                    centeredSlides: false,
                    paginationClickable: true,
                });
                this.clickNavClassify(nav_classify);
            }
            var objs = obj.querySelectorAll("li.one_level li");
            $(objs).off().on("click",function(){
                if(obj.querySelector("li.one_level li.on")){
                    if (this.className === "on") {
                        this.classList.remove("on");
                        document.getElementById("view_area").classList.remove("on");
                        return true;
                    }
                    if(this.parentNode.parentNode.getAttribute("ref") !== "right_classify"){
                        obj.querySelector("li.one_level li.on").classList.remove("on");
                    }
                    document.getElementById("view_area").classList.remove("on");
                }
                this.classList.add("on");
                // document.getElementById("bottom_map").src = + ".raw";
                document.getElementById("view_area").classList.add("on");
                document.getElementById("bottom_map").src = this.querySelector("img").src;
                // document.getElementById("bottom_map").src = this.querySelector("img").src + '.raw';
            })
        },
        clickNavClassify:function(obj){
            $(obj.querySelectorAll("span.swiper-slide")).off().on("click",function(){
                var selected_objs = obj.querySelector("div.swiper-wrapper span.on");
                selected_objs.classList.remove("on");
                this.classList.add("on")
    
                // _this.initRightClass($(__this.parentNode).attr('link'), function() {
                //     run();
                // });
            })
        }
    }
    return module;
});